package com.sell.demo.util;

import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

/**
 * Created by Mingo on 2017/6/14.
 */
public class RegionCodeUtil {

    /**
     * 判断省
     * @param code
     * @return
     */
    public static boolean isProvince(String code) {
        int length = StringUtils.length(StringUtils.stripEnd(code, "0"));
        return length > 0 && length < 3;
    }

    /**
     * 判断市
     * @param code
     * @return
     */
    public static boolean isCity(String code) {
        int length = StringUtils.length(StringUtils.stripEnd(code, "0"));
        return length > 2 && length < 5;
    }

    /**
     * 判断区县
     * @param code
     * @return
     */
    public static boolean isCounty(String code) {
        int length = StringUtils.length(StringUtils.stripEnd(code, "0"));
        return length > 4 && length < 7;
    }

    /**
     * 判断a是否包含b
     * @param a
     * @param b
     * @return
     */
    public static boolean isIn(String a, String b) {
        if (!isValid(a) || !isValid(b)) {
            return false;
        }
        a = StringUtils.stripEnd(a, "0");
        if (a.length() % 2 != 0) {
            a = StringUtils.rightPad(a, a.length() + 1, "0");
        }
        return b.startsWith(a);
    }

    /**
     * 判断是否是有效的行政区划代码
     * @param code
     * @return
     */
    public static boolean isValid(String code) {
        return Pattern.matches("^[1-9]\\d{5}$", code);
    }
}
